//$URL: https://dasding.googlecode.com/svn/trunk/api/src/main/java/de/piratech/dasding/api/rest/EventResource.java $
//$Id: EventResource.java 117 2013-03-30 23:27:57Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.api.rest;

import java.util.List;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.piratech.dasding.data.Location;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.security.annotations.RequireUserRole;
import de.piratech.dasding.services.Geocoder;
import de.piratech.dasding.services.LocationFilter;

@Path("/locations")
@Component
public class LocationResource extends AbstractResource {

  private static final Logger LOG = LoggerFactory
      .getLogger(LocationResource.class);

  @Autowired
  private LocationFilter locationFilter;

  @Autowired
  private Geocoder geocoder;

  @GET
  @Path("/find") 
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  public List<Location> findLocations(
      @QueryParam(value = "lat") Float pLatitude,
      @QueryParam(value = "lon") Float pLongitude,
      @DefaultValue(value = "5") @QueryParam(value = "distance") Float pDistance) {
    LOG.debug("findlocation(lat={}, lon={}, distance={})", pLatitude,
        pLongitude, pDistance);
    try {
      // TODO filter location by parameters
      return getLocationData().getLocations();
    } catch (DatabaseException dbe) {
      throw getBadRequestException(dbe);
    }
  }

  /**
   * Creates a new event
   * 
   * http://localhost:8080/dasding.api/api/events
   * 
   * POST
   * 
   * @param pName
   *          (required)
   * @param pDescription
   *          (required)
   * @param pCategoryID
   *          (required)
   * @param pTime
   *          (required)
   * @param pLatitude
   *          (required)
   * @param pLongitude
   *          (required)
   * @return
   */
  @POST
  @RequireUserRole(roles = User.USER_ROLE.USER)
  public Response createLocation(@QueryParam("latitude") double pLatitude,
      @QueryParam("longitude") double pLongitude,
      @QueryParam("name") String name, @Context SecurityContext pSecurityContext) {
    try {
      //TODO create valid location
      Location location = new Location();
      location.setLongitude(pLongitude);
      location.setLatitude(pLatitude);
      location.setName(name);

      if (!location.isValid()) {
        return getBadRequestResponse();
      }
      location = getLocationData().createLocation(location);
      return getCreatedResponse("/id/" + location.getId());
    } catch (DatabaseException dbe) {
      return getBadRequestResponse(dbe);
    }
  }

  @Override
  protected Logger getLogger() {
    return LOG;
  }
}
